import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
// import 'package:simple_native_image_compress/simple_native_image_compress.dart';
import 'package:study_demo/base/BaseController.dart';
import 'package:path/path.dart' as p;

class PictureFormatChangeController extends BaseController {
  final imageFormats = ['.jpeg', '.webp'];
  final imagePath = ''.obs;
  final intPutImageFormat = '.png'.obs;
  final imageFormat = '.webp'.obs;
  final imageSize = ''.obs;
  final outputImagePath = ''.obs;
  final outputImageFormat = '.png'.obs;
  final outputImageSize = ''.obs;

  final TextEditingController _textEditingController =
      TextEditingController(text: '75');

  TextEditingController get textEditingController => _textEditingController;

  changeImageFormat(String value) {
    imageFormat.value = value;
  }

  choosePicture() async {
    final result = await FilePicker.platform.pickFiles();
    if (result != null) {
      final path = result.files.single.path;
      imagePath.value = path!;
      final fileName = p.basename(imagePath.value);
      intPutImageFormat.value = p.extension(fileName);
      int length = File(path).lengthSync();
      imageSize.value = (length / (1024 * 1024)).toStringAsFixed(2) +
          'MB' +
          '(' +
          (length / 1024).toStringAsFixed(0) +
          'KB)';
      logger.i('fileName:${imageFormat.value}');
    }
  }

  startTransform() async {
    String src = imagePath.value;
    final directory = p.dirname(src);
    final fileName = p.basenameWithoutExtension(src);
    logger.i('fileName:$fileName');
    logger.i('directory:$directory');
    Directory tempDirectory = Directory(directory + '\\output');
    if (!tempDirectory.existsSync()) {
      tempDirectory.createSync();
    }
    logger.i('tempDirectory:$tempDirectory');
    final newName = p.join(tempDirectory.path, fileName + imageFormat.value);
    logger.i('newName:$newName');
    final outputFile = File(newName);
    if (!outputFile.existsSync()) {
      outputFile.createSync();
    }
    // var convertedImageBytes = await convertImageToWebp(src);
    // await outputFile.writeAsBytes(convertedImageBytes);
    // // convertToJpg(
    // //     inputPath: imagePath.value, outputPath: outputFile.path, quality: 75);
    // outputImagePath.value = outputFile.path;
    // outputImageFormat.value = imageFormat.value;
    // int length = outputFile.lengthSync();
    // outputImageSize.value = (length / (1024 * 1024)).toStringAsFixed(2) +
    //     'MB' +
    //     '(' +
    //     (length / 1024).toStringAsFixed(0) +
    //     'KB)';
    // toast('转换成功');
  }

  // Future<List<int>> convertImageToWebp(String path) async {
  //   final quality = int.parse(_textEditingController.text.toString());
  //   logger.i('quality:$quality');
  //   final result = await ImageCompress.contain(
  //     filePath: path,
  //     quality: quality,
  //     maxWidth: 2048,
  //     maxHeight: 2048,
  //     compressFormat: CompressFormat.webP,
  //     samplingFilter: FilterType.lanczos3,
  //   );
  //   return result;
  // }

  delete({int type = 0}) {
    if (type == 0) {
      imagePath.value = '';
    } else {
      outputImagePath.value = '';
    }
  }
}
